package com.cellngine;

import java.util.ArrayList;

/**
 * MultiLogger
 * 
 * Simplifies sending log messages to multiple loggers at once.
 * 
 * @author Felix Kirchmann <hellraz0r.386@googlemail.com>
 */
public class MultiLogger
{
	private ArrayList<Logger>	loggers;
	
	/**
	 * Adds a new Logger to the distribution list.
	 * 
	 * @param logger
	 *            The new <code>logger</code> to add, all further log messages sent to this object
	 *            will be distributed to <code>logger</code> too.
	 */
	public void addLogger(final Logger logger)
	{
		if (logger == null)
		{
			throw new NullPointerException();
		}
		
		if (this.loggers == null)
		{
			this.loggers = new ArrayList<Logger>();
		}
		
		this.loggers.add(logger);
	}
	
	/**
	 * Removes a Logger from the distribution list.
	 * 
	 * @param logger
	 * @return True if the loger was found on the list and successfully removed, otherwise false.
	 */
	public boolean removeLogger(final Logger logger)
	{
		if (this.loggers.contains(logger))
		{
			this.loggers.remove(logger);
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * Sends a log message to all loggers on the distribution list.
	 * 
	 * @param o
	 *            What to log.
	 * @param type
	 *            The type of the log entry - see Constants.log_type for details.
	 */
	public synchronized void log(final Object o, final Constants.log_type type)
	{
		for (int i = 0; i < this.loggers.size(); i++)
		{
			if (this.loggers.get(i) != null)
			{
				this.loggers.get(i).log(o, type);
			}
		}
	}
}
